//
//  BaseTableViewController.m
//  mike2.0
//
//  Created by mike on 15/9/7.
//  Copyright (c) 2015年 flychi. All rights reserved.
//

#import "BaseTableViewController.h"

@interface BaseTableViewController ()

/**
 *  是否左缩进
 */

@property (nonatomic, assign) BOOL isSeparatorZero;

@end

@implementation BaseTableViewController

#pragma mark - Life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)deallocRelease{
    [super deallocRelease];
    _dataSource = nil;
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    _tableView = nil;
}

#pragma mark - Propertys

-(UIView *)blankHintView{
    if (!_blankHintView) {
        [self initBlankHintView];
    }
    return _blankHintView;
}

-(void)initBlankHintView{
    if (self.offsetY) {
        _blankHintView=[[UIView alloc]initWithFrame:CGRectMake(0, VIEW_H(self.tableView.tableHeaderView), self.view.frame.size.width, self.view.frame.size.height - VIEW_H(self.tableView.tableHeaderView)-VIEW_H(self.tableView.tableFooterView))];
    }else{
        _blankHintView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    }
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(VIEW_W(self.tableView)/2-40,VIEW_H(_blankHintView)/2-40-30,80,80)];
    if (_blankHintImage) {
        imageView.image = _blankHintImage;
    }else{
        imageView.image = [UIImage imageNamed:@"mine_nodata"];
    }
    imageView.contentMode=UIViewContentModeScaleAspectFit;
    UILabel *hintLabel = [[UILabel alloc]initWithFrame:CGRectMake(VIEW_TX(imageView)-10, VIEW_TY(imageView)+VIEW_H(imageView), 200, 30)];
    hintLabel.numberOfLines = 0;
    hintLabel.lineBreakMode = NSLineBreakByWordWrapping;
    hintLabel.textColor=[UIColor colorWithHexString:@"9e9e9e"];
    hintLabel.backgroundColor = [UIColor clearColor];
    hintLabel.textAlignment=NSTextAlignmentLeft;
    hintLabel.font=FONT(15);
    hintLabel.text=_blankHintString;
    [_blankHintView addSubview:hintLabel];
    [_blankHintView addSubview:imageView];
//    [_tableView addSubview:hintLabel];
//    [_tableView addSubview:hintLabel];
    [hintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom).offset(-60);
        make.centerX.equalTo(imageView.mas_centerX).offset(0);
        make.height.equalTo(@30);
    }];
}

#pragma mark - Data Source

-(void)loadDefaultDataSource{
    [super loadDefaultDataSource];
    _dataSource = [[NSMutableArray alloc] initWithCapacity:1];
}

#pragma mark - UITableView DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_isMultiSection) {
        return _dataSource.count;
    }else{
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_isMultiSection) {
        return [_dataSource[section] count];
    }else{
        return _dataSource.count;
    }
//    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // in subClass
    return nil;
}

#pragma mark - UITableView Delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _selectIndexPath=indexPath;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isSeparatorZero) {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    }
}

#pragma mark - Public Methods

-(void)initTableViewWithStyle:(UITableViewStyle)tableViewStyle{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:tableViewStyle];
    _tableView.tableFooterView = [UIView new];
    _tableView.delegate=self;
    _tableView.dataSource=self;
}

- (void)configuraTableViewExtraSeparator{
    self.tableView.tableFooterView = [[UIView alloc]init];
}

- (void)configuraTableViewNormalSeparatorInset {
    _isSeparatorZero=YES;
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)reloadDataWithBlank{
    if (_isMultiSection) {
        BOOL isCountZero=YES;
        for (NSArray *array in self.dataSource) {
            if (array.count!=0) {
                isCountZero=NO;
                break;
            }
        }
        if (isCountZero) {
            [_tableView addSubview:self.blankHintView];
        }else{
            [self.blankHintView removeFromSuperview];
        }
    }
    else{
        if (self.dataSource.count==0) {
            [_tableView addSubview:self.blankHintView];
        }else{
            [self.blankHintView removeFromSuperview];
        }
    }
    [_tableView reloadData];
}

-(UILabel *)nolabel{
    if (!_nolabel) {
        _nolabel = [UILabel new];
        _nolabel.font = FONT(13);
        _nolabel.text = @"暂无内容";
        _nolabel.textColor = XFUIColorFromRGB(0x999999);
        _nolabel.textAlignment = NSTextAlignmentCenter;
        [self.tableView addSubview:_nolabel];
//        [self.view bringSubviewToFront: _tab_nolabel];
        [_nolabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.width.mas_equalTo(SCREEN_WIDTH);
            make.top.mas_equalTo(340+(SCREEN_HEIGHT - 340 - 44-100) * 0.5);
            make.height.mas_equalTo(50);
        }];
    }
    return _nolabel;
}

-(UILabel *)searchNoLabel{
    if (!_searchNoLabel) {
        _searchNoLabel =[UILabel new];
        _searchNoLabel.text = @"没有找到搜索结果喔~";
        _searchNoLabel.textColor = XFUIColorFromRGB(0x999999);
        _searchNoLabel.textAlignment = NSTextAlignmentCenter;
        [self.tableView addSubview:_searchNoLabel];
        [_searchNoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.width.mas_equalTo(SCREEN_WIDTH);
            make.top.mas_equalTo(60);
            make.height.mas_equalTo(50);
        }];
    }
    return _searchNoLabel;
}
@end
